package com.tiyende.kash.loop.ui.dialog

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.common.CommonDialog
import com.tiyende.kash.loop.databinding.TiyendeDialogPhotoBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：拍照、相册选择 dialog
 */
class TiyendePhotoDialog: CommonDialog {

    private lateinit var binding: TiyendeDialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""
    //默认选择拍照
    private var selecetedType = 1

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        eventRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {
        selecetedType = 1
        checkTiyendeSelectType()
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.imageViewTipsClose)//关闭
        setOnClickListener(binding.btCamera) //拍照
        setOnClickListener(binding.btAlbum) //相册
        setOnClickListener(binding.btConfirm)//确定
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        when(v?.id){
            R.id.btCamera -> {//拍照
                selecetedType = 1
                checkTiyendeSelectType()
            }
            R.id.btAlbum -> {//相册选择
                selecetedType = 2
                checkTiyendeSelectType()
            }
            R.id.btConfirm -> {//确定
                if (selecetedType == 1){
                    mCallback?.startCameraAction()
                } else {
                    mCallback?.startAlbumAction()
                }
                dismiss()
            }
            else -> {
                dismiss()
            }
        }
    }

    /**
     * 设置埋点备注
     */
    fun addTiyendeTipsEventRemark(remark: String){
        eventRemark = remark
    }

    /**
     * 切换拍照、相册选择
     */
    private fun checkTiyendeSelectType(){
        binding.btCamera.shapeDrawableBuilder
            .setStrokeColor(Color.parseColor("#FF000000"))
            .intoBackground()
        binding.imaCameraSelected.setImageResource(R.mipmap.tiyende_gender_normal_icon)

        binding.btAlbum.shapeDrawableBuilder
            .setStrokeColor(Color.parseColor("#FF000000"))
            .intoBackground()
        binding.imaAlbumSelected.setImageResource(R.mipmap.tiyende_gender_normal_icon)
        when(selecetedType){
            1 -> {
                binding.btCamera.shapeDrawableBuilder
                    .setStrokeColor(Color.parseColor("#FF3ED9AB"))
                    .intoBackground()
                binding.imaCameraSelected.setImageResource(R.mipmap.tiyende_gender_selected_icon)
            }
            2 -> {
                binding.btAlbum.shapeDrawableBuilder
                    .setStrokeColor(Color.parseColor("#FF3ED9AB"))
                    .intoBackground()
                binding.imaAlbumSelected.setImageResource(R.mipmap.tiyende_gender_selected_icon)
            }
        }
    }

    interface IDialogCallback {
        /* 拍照 */
        fun startCameraAction()
        /* 相册 */
        fun startAlbumAction()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_TIPS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_TIPS_DISMISS,eventRemark)
    }
}